<?php
session_start();

	class block_twitter_users_map extends block_base
	{
	
	
		function init() {
        	// standard code to set the block title and version
        	$this->title = 'Twitter Map';
        	$this->version = 2009012000;
	    }
		
	    function get_content()
	    {	    	
	    	if ($this->content !== NULL)
	    	{
	        	return $this->content;
	    	}
	
	    	$this->content = new stdClass;
	    	
	    	// To get wwwroot we must have access to the $CFG
			global $CFG;
			
			$thisroot = $CFG->wwwroot.$this->config->block_twitter_users_map_path;	
			$apikey = $this->config->block_twitter_users_map_google_api_key;
			$showoffline = $this->config->block_twitter_users_map_show_offline;
			
			// write Javascript to handle the submission, and HTML to render the form
			// (the Javascript uses AJAX to send the data to lib.php, which actually posts the data to Twitter)
			$output = "<html>
				<body>
				
				<script src=\"http://maps.google.com/maps?file=api&amp;v=2.x&amp;key=".$apikey."\" type=\"text/javascript\"></script>				
				<script src=\"http://www.google.com/uds/api?file=uds.js&amp;v=1.0&amp;key=".$apikey."\" type=\"text/javascript\"></script>
				<script src=\"http://www.google.com/uds/solutions/mapsearch/gsmapsearch.js\" type=\"text/javascript\"></script>
				<div id=\"block_online_users_googlemap\" style=\"text-align:center;height:150px;width:200px;overflow:hidden; margin-bottom:10px;\">Click \"Update Map\" To see where people are Twittering from!</div>
				<hr />
				
				<script type=\"text/javascript\">
				
				var map = null;
				
				function JSONscriptRequest(fullUrl) {
					this.fullUrl = fullUrl; 
					this.noCacheIE = \"&noCacheIE=\" + (new Date()).getTime();
					this.headLoc = document.getElementsByTagName(\"head\").item(0);
					this.scriptId = \"YJscriptId\" + JSONscriptRequest.scriptCounter++;
				}
				
				JSONscriptRequest.scriptCounter = 1;
				
				JSONscriptRequest.prototype.buildScriptTag = function () {
				
					this.scriptObj = document.createElement(\"script\");
					
					this.scriptObj.setAttribute(\"type\", \"text/javascript\");
					this.scriptObj.setAttribute(\"src\", this.fullUrl + this.noCacheIE);
					this.scriptObj.setAttribute(\"id\", this.scriptId);
				}
				 
				JSONscriptRequest.prototype.removeScriptTag = function () {
					this.headLoc.removeChild(this.scriptObj);  
				}
				
				JSONscriptRequest.prototype.addScriptTag = function () {
					this.headLoc.appendChild(this.scriptObj);
				}
								
				function loadMap(){
					if(document.getElementById(\"block_online_users_googlemap\") != null){
						if (GBrowserIsCompatible()) {
							map = new GMap2(document.getElementById(\"block_online_users_googlemap\"));
							map.addControl(new GSmallZoomControl());
							map.setCenter(new GLatLng(50.9,-1.4));
							getMapUsers();
						}	 
					}					
				}
				
				function loadUsersCallback(jData){
					if(!jData){
						return;
					}
					var evalText = eval(\"(\" + jData + \")\");
					var users = evalText['online'];
					if(!users){
						return;
					}
					for (i=0; i<users.length; i++){
						createMarker(users[i]);
					}
				}
				
				function createMarker(user){
					if (user.lat != \"\" && user.lng != \"\"){
						var point = new GLatLng(user.lat, user.lng);
				        if(user.online == \"true\"){
						    map.addOverlay( createOnlineMarker(point,user));
				        } else {
				            if(".$showoffline." == \"1\")
				            {
				            	map.addOverlay( createOfflineMarker(point,user));
				            }
				        }
					}
				}
				
				function createOnlineMarker(point,user){
					var marker = new GMarker(point, {icon: new OnlineMoodleBlockMapMarker(), title: user.fullname + \", \" + user.location});
					return marker;
				}
				
				function OnlineMoodleBlockMapMarker(){
					var icon = new GIcon();
					icon.image = \"" . $thisroot . "images/online.png\";
					icon.shadow = \"" . $thisroot . "images/shadow.png\";
					icon.iconSize = new GSize(22, 15);
					icon.shadowSize = new GSize(22, 15);
					icon.iconAnchor = new GPoint(7, 14);
					icon.infoWindowAnchor = new GPoint(13,5);  
					return icon; 
				}
				
				function createOfflineMarker(point,user){
				    var marker = new GMarker(point, {icon: new OfflineMoodleBlockMapMarker(), title: user.fullname + \", \" + user.location});
				    return marker;
				}
				
				function OfflineMoodleBlockMapMarker(){
				    var icon = new GIcon();
				    icon.image = \"" . $thisroot . "images/offline.png\";
				    icon.shadow = \"" . $thisroot . "images/shadow.png\";
				    icon.iconSize = new GSize(22, 15);
				    icon.shadowSize = new GSize(22, 15);
				    icon.iconAnchor = new GPoint(7, 14);
				    icon.infoWindowAnchor = new GPoint(13,5);  
				    return icon; 
				}

				function get_cookie ( cookie_name )
				{
				  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
				
				  if ( results )
				    return ( unescape ( results[2] ) );
				  else
				    return null;
				}
				
				function getMapUsers()
				{
					if(document.ajaxMapForm.ajaxMapFeed.value == \"My friends\")
					{
						if(document.ajaxMapForm.ajaxMapPass.value == \"\")
						{
							document.getElementById(\"block_online_users_googlemap\").innerHTML = \"You must supply a username and password when requesting your 'friends' feed\";
						}
						else
						{						
							var xmlHttp;
							
							document.cookie = \"ajaxMapUser=\" + escape(document.ajaxMapForm.ajaxMapUser.value) + \";path=/\";
							document.cookie = \"ajaxMapPass=\" + escape(document.ajaxMapForm.ajaxMapPass.value) + \";path=/\";
							document.cookie = \"ajaxMapFeed=\" + escape(document.ajaxMapForm.ajaxMapFeed.value) + \";path=/\";
												
							try
							{
								xmlHttp = new XMLHttpRequest();
							}
							catch(e)
							{
								try
								{
									xmlHttp = new ActiveXObject(\"Msxml2.XMLHTTP\");
								}
								catch(e)
								{
									try
									{
										xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\");
									}
									catch(e)
									{
										alert(\"Your browser does not support AJAX!\");
										return false;
									}
								}
							}
							
							xmlHttp.onreadystatechange=function()
							{
								if(xmlHttp.readyState==4)
								{
									var response = loadUsersCallback(xmlHttp.responseText);
									aObj = new JSONscriptRequest(loadUsersCallback(xmlHttp.responseText));
									aObj.buildScriptTag();
									aObj.addScriptTag();
								}
							}
							
							xmlHttp.open(\"GET\", \"". $thisroot ."lib.php\", true);
							xmlHttp.send(null);
						}
					}
					else
					{						
						var xmlHttp;
						
						document.cookie = \"ajaxMapUser=\" + escape(document.ajaxMapForm.ajaxMapUser.value) + \";path=/\";
						document.cookie = \"ajaxMapPass=\" + escape(document.ajaxMapForm.ajaxMapPass.value) + \";path=/\";
						document.cookie = \"ajaxMapFeed=\" + escape(document.ajaxMapForm.ajaxMapFeed.value) + \";path=/\";
											
						try
						{
							xmlHttp = new XMLHttpRequest();
						}
						catch(e)
						{
							try
							{
								xmlHttp = new ActiveXObject(\"Msxml2.XMLHTTP\");
							}
							catch(e)
							{
								try
								{
									xmlHttp = new ActiveXObject(\"Microsoft.XMLHTTP\");
								}
								catch(e)
								{
									alert(\"Your browser does not support AJAX!\");
									return false;
								}
							}
						}
						
						xmlHttp.onreadystatechange=function()
						{
							if(xmlHttp.readyState==4)
							{
								var response = loadUsersCallback(xmlHttp.responseText);
								aObj = new JSONscriptRequest(loadUsersCallback(xmlHttp.responseText));
								aObj.buildScriptTag();
								aObj.addScriptTag();
							}
						}
						
						xmlHttp.open(\"GET\", \"". $thisroot ."lib.php\", true);
						xmlHttp.send(null);
					}
				}				
								
				</script>
				
				<form name=\"ajaxMapForm\">
   				Username: <input type=\"text\" name=\"ajaxMapUser\" value=\"\"/><br />
    			Password: <input type=\"password\" name=\"ajaxMapPass\"/><p  value=\"\"/>
    			Feed: <select name=\"ajaxMapFeed\">
	 				<option>My friends</option>
	 				<option>Public</option>
 				</select><p />
 				<center>
 				<input type=\"button\" value=\"Update Map\" onClick=\"loadMap();\" />
 				</center>
 				</form>
 				
 				
 				<script type=\"text/javascript\">
				
					document.ajaxMapForm.ajaxMapUser.value = get_cookie(\"ajaxMapUser\");
					document.ajaxMapForm.ajaxMapPass.value = get_cookie(\"ajaxMapPass\");
					
				</script>
 				
				</body>
				</html>";
				
			// set this Javascript/HTML content as the block's output, and display it
			$this->content->text = $output;
	    	return $this->content;
				
	    }

		function instance_allow_config()
		{
	    	// allow instance-based block configuration
	    	return true;
		}
		
		function has_config()
		{
			return false;
		}
		
		function instance_allow_multiple()
		{
    		// allow multiple instances of the block
    		return true;
		}
		
		function preferred_width() {
			// set the preferred value in pixels
			return 200;
	    }
	    
	}
	
?>